package com.quectel.cms.controller.application;


import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.application.dto.ApplicationAccessConfDto;
import com.quectel.core.module.application.service.ApplicationAccessConfService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author rananxin
 * @date 2021-11-19 14:20:25
 */
@RestController
@RequestMapping("applicationAccessConf")
@Tag(name = "ApplicationAccessConfController", description = "接入方式相关api")
public class ApplicationAccessConfController extends BaseController {

    @DubboReference
    private ApplicationAccessConfService applicationAccessConfService;

    /**
     * 列表
     */
    @GetMapping("list")
    @RequirePermissions(values = "applicationAccessConf:list")
    @Operation(summary = "接入方式的分页查询列表")
    public Response<Page<ApplicationAccessConfDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest)).like("name");

        super.fillAuthorityData(request.getParams());
        int total = applicationAccessConfService.queryTotal(request.getParams());

        List<ApplicationAccessConfDto> list = null;
        if (total > 0) {
            list = applicationAccessConfService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<ApplicationAccessConfDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<ApplicationAccessConfDto>>ok().wrap(page);
    }


    /**
     * 信息
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "applicationAccessConf:info")
    @Operation(summary = "查询单条接入方式记录")
    public Response<ApplicationAccessConfDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (applicationAccessConfService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        ApplicationAccessConfDto applicationAccessConfDto = applicationAccessConfService.selectById(id);

        return Response.<ApplicationAccessConfDto>ok().wrap(applicationAccessConfDto);
    }

    /**
     * 保存
     */
    @PostMapping("save")
    @RequirePermissions(values = "applicationAccessConf:edit")
    @Operation(summary = "保存单条接入方式记录")
    public Response<Object> save(@RequestBody @Validated ApplicationAccessConfDto applicationAccessConfDto) {

        //填充基础数据
        fillSaveCommonData(applicationAccessConfDto);

        applicationAccessConfService.save(applicationAccessConfDto);

        return Response.ok();
    }

    /**
     * 修改
     */
    @PostMapping("update")
    @RequirePermissions(values = "applicationAccessConf:edit")
    @Operation(summary = "修改单条接入方式记录")
    public Response<Object> update(@RequestBody @Validated ApplicationAccessConfDto applicationAccessConfDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(applicationAccessConfDto.getId()));
        if (applicationAccessConfService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(applicationAccessConfDto);

        applicationAccessConfService.updateById(applicationAccessConfDto);

        return Response.ok();
    }

    /**
     * 删除
     */
    @PostMapping("delete")
    @RequirePermissions(values = "applicationAccessConf:delete")
    @Operation(summary = "批量删除存在的接入方式记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (applicationAccessConfService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        applicationAccessConfService.deleteBatch(ids);

        return Response.ok();
    }
}
